﻿using Models.WebChat;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;

namespace Business.Uin
{
    /// <summary>
    /// 用户 socket 管理
    /// 用户登录后的socket 信息保存在这里
    /// </summary>
    public class UinSocketManager
    {
        private static List<UinSocket> _UinSocketList = null;
        private static object _Lock = new object();
        static UinSocketManager()
        {
            _UinSocketList = new List<UinSocket>();
        }

        public void Add(UinSocket uinSocket)
        {
            lock (_Lock)
            {
                _UinSocketList.Add(uinSocket);
            }
        }
        public void Remove(UinSocket uinSocket)
        {
            lock (_Lock)
            {
                if (_UinSocketList.Contains(uinSocket))
                    _UinSocketList.Remove(uinSocket);
            }
        }
        public void Remove(int uinid)
        {
            lock (_Lock)
            {
                var uinSocket = _UinSocketList.Where(us => us.UinId == uinid).FirstOrDefault();
                if (uinSocket != null) _UinSocketList.Remove(uinSocket);
            }
        }
        public int Count()
        {
            return _UinSocketList.Count;
        }
        public List<UinSocket> GetUinSocketListByUinIdList(params int[] uinIdList)
        {
            return _UinSocketList.Where(u => uinIdList.Contains(u.UinId)).ToList();
        }
    }
}
